package com.scwatch.core.service.impl;

import com.scwatch.core.domain.Info;
import com.scwatch.core.domain.InfoNode;
import com.scwatch.core.domain.InfoNode.InfoNodeId;
import com.scwatch.core.domain.Node;
import com.scwatch.core.repository.InfoDao;
import com.scwatch.core.repository.InfoNodeDao;
import com.scwatch.core.repository.NodeDao;
import com.scwatch.core.service.InfoNodeService;
import com.scwatch.core.service.NodeQueryService;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;

@Service
@Transactional(readOnly = false, isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED)
public class InfoNodeServiceImpl implements InfoNodeService {
	private InfoNode findOrCreate(Info info, Node node) {
		InfoNode bean = dao.findOne(new InfoNodeId(info.getId(), node.getId(),"1"));
		if (bean == null) {
			bean = new InfoNode(info, node,"1");
		}
		return bean;
	}

	@Transactional
	public List<InfoNode> update(Info info, Integer[] nodeIds, Integer nodeId) {
		// 主栏目为null，不更新。
		if (nodeId == null) {
			return null;
		}
		if (nodeIds == null) {
			nodeIds = new Integer[0];
		} else {
			nodeIds = ArrayUtils.removeElement(nodeIds, nodeId);
		}
		List<InfoNode> infoNodes = info.getInfoNodes();
		infoNodes.clear();
		infoNodes.add(findOrCreate(info, nodeService.get(nodeId)));
		for (Integer id : nodeIds) {
			infoNodes.add(findOrCreate(info, nodeService.get(id)));
		}
		return infoNodes;
	}
    @Transactional
    public List<InfoNode> updateRef(Info info, Integer[] nodeIds, Integer nodeId,Integer sort) {
        // 主栏目为null，不更新。
        if (nodeId == null) {
            return null;
        }
        if (nodeIds == null) {
            nodeIds = new Integer[0];
        } else {
            nodeIds = ArrayUtils.removeElement(nodeIds, nodeId);
        }
        List<InfoNode> infoNodes = info.getInfoNodes();
//        List<Integer> paramList=new ArrayList<Integer>();
//        paramList.add(nodeId);
//        List<Info> infoList=infoDao.findByNodeIdIn(paramList);
        //重置顺序
//        if(sort==null){
//            sort=infoList==null?1:infoList.size()+1;  //顺序加1
//        }else{
//           if(infoList!=null && infoList.size()>0){
//               for(Info o :infoList){
//                 if(o.getInfoNodes().get){
//
//                 }
//               }
//           }
//        }
//
//        for(InfoNode infoNode:infoNodes){
//            //每次只引用到一个栏目 的情况
//            if(infoNode.getNode().getId().equals(nodeId)){
//               if(sort==null){  //顺序加1
//                   index++;
//               }else{
////                  if(infoNode.getSort().equals()){
////
////                  }
//               }
//            }
//        }

//        infoNodes.clear();
        infoNodes.add(selectOrCreate(info, nodeService.get(nodeId),"0"));
        for (Integer id : nodeIds) {
            infoNodes.add(selectOrCreate(info, nodeService.get(id),"0"));
        }
        return infoNodes;
    }

    private InfoNode selectOrCreate(Info info, Node node,String reference) {
        InfoNode bean = dao.findOne(new InfoNodeId(info.getId(), node.getId(),reference));
        if (bean == null) {
            bean = new InfoNode(info, node,reference);
        }
        return bean;
    }

	@Transactional
	public int moveByNodeId(Collection<Integer> nodeIds, Integer nodeId) {
		return dao.moveByNodeId(nodeIds, nodeId);
	}

	@Override
	@Transactional
	public void deleteByNodeId(Integer nodeId) {
		Node node = nodeService.get(nodeId);
		for (Info info : dao.findByNodesNodeId(nodeId)) {
			InfoNode infoNode = new InfoNode(info, node,null);
			info.getInfoNodes().remove(infoNode);
			dao.delete(infoNode);
		}
	}

    @Override
    @Transactional
    public String  deleteByNodeIdAndInfoId(Integer nodeId,Integer infoId) {
        Node node = nodeService.get(nodeId);
        Info info= infoDao.findOne(infoId);
        if(info==null){
            return null;
        }
        List<InfoNode> infoNodes=info.getInfoNodes();
        String ref=null;
        for(InfoNode o:infoNodes){
            if(infoId.equals(o.getInfo().getId()) && nodeId.equals(o.getNode().getId()) ){
                ref=o.getReference();
            }
        }
        InfoNode infoNode = new InfoNode(info, node,ref);
        info.getInfoNodes().remove(infoNode);
        dao.delete(infoNode);
        return  ref;
    }

    @Override
    public int updateNodeInfoStickAndPic(Integer targetNodeId, Integer infoId, Boolean isStick, Boolean bigPic) {
	    String picStatus = bigPic?"1":"0";
	    String stickStatus = isStick?"1":"0";
	    int result = 0;
	    //置顶
        if(isStick){
            dao.updateUnStick(targetNodeId);
            result = dao.updateStick(targetNodeId,infoId,stickStatus,picStatus);
        }else{
            result = dao.updateStick(targetNodeId,infoId,stickStatus,picStatus);
        }
        return result;
    }

    private NodeQueryService nodeService;

	@Autowired
	public void setNodeQueryService(NodeQueryService nodeService) {
		this.nodeService = nodeService;
	}

	// private InfoQueryService infoService;
	//
	// @Autowired
	// public void setInfoService(InfoQueryService infoService) {
	// this.infoService = infoService;
	// }

	private InfoNodeDao dao;
	private  InfoDao infoDao;
	private NodeDao nodeDao;

	@Autowired
	public void setDao(InfoNodeDao dao) {
		this.dao = dao;
	}

    @Autowired
    public void setInfoDao(InfoDao infoDao) {
        this.infoDao = infoDao;
    }

    @Autowired
    public void setInfoDao(NodeDao nodeDao) {
        this.nodeDao = nodeDao;
    }
}
